package com.kzj.controller;

import com.kzj.annotation.OnCreate;
import com.kzj.annotation.OnEdit;
import com.kzj.common.constant.GlobalConstant;
import com.kzj.common.enumerate.RoleEnum;
import com.kzj.common.result.Result;
import com.kzj.pojo.Student;
import com.kzj.service.StudentService;
import com.kzj.service.jpaRepository.StudentJpaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
//给该Controller均加上URL路径前缀
@RequestMapping("/student")
public class StudentController {
    @Autowired
    StudentJpaRepository studentJpaRepository;
    @Autowired
    StudentService studentService;
    @GetMapping
    public Result getAll()
    {
        return Result.success(studentJpaRepository.findAll());
    }
    @GetMapping("/{studentId}")
    public Result getById(@PathVariable("studentId")Student student)
    {
        return Result.success(student);
    }
    @PutMapping
    public Result update(@RequestBody @Validated(OnEdit.class) Student student)
    {
        studentJpaRepository.save(student);
        return Result.success();
    }
    @PostMapping
    public Result add(@RequestBody @Validated(OnCreate.class) Student student)
    {
        studentJpaRepository.save(student);
        return Result.success();

    }
    @DeleteMapping("/{studentId}")
    public Result deleteById(@PathVariable("studentId") Long studentId)
    {
        studentJpaRepository.deleteById(studentId);
        return Result.success();
    }
    @GetMapping("/getStudentByCourseIds/{courseIds}")
    public Result getStudentByCourseIds(@PathVariable("courseIds") String courseIds)
    {
        List<Long> courseIdList = Arrays.stream(courseIds.split(",")).map(Long::valueOf).collect(Collectors.toList());
        return studentService.getStudentByCourseIds(courseIdList);
    }
    @GetMapping("/getStudentTeacherVoByTeacher")
    public Result getStudentByTeacher(HttpSession session)
    {
        Long teacherId=(Long) session.getAttribute(GlobalConstant.ID);
        if(RoleEnum.TEACHER!=session.getAttribute(GlobalConstant.ROLE))
            throw new RuntimeException("角色异常");
        return studentService.getStudentTeacherVoByTeacherId(teacherId);
    }
    @GetMapping("/getNotScoreStudentsByCourseId/{courseId}")
    public Result getNotScoreStudentsByCourseId(@PathVariable("courseId") Long courseId)
    {
        return studentService.getNotScoreStudentsByCourseId(courseId);
    }
    @GetMapping("/getStudentTeacherVoByCourseIds/{courseIds}")
    public Result getStudentTeacherVoByCourseCourseIds(@PathVariable("courseIds") String courseIds)
    {
        List<Long> courseIdList = Arrays.stream(courseIds.split(",")).map(Long::valueOf).collect(Collectors.toList());
        return studentService.getStudentTeacherVoByCourseIds(courseIdList);
    }
    @GetMapping("/getNotScoreStudentsByCourseIdAndTerm")
    public  Result getNotScoreStudentsByCourseIdAndTerm(Long courseId,Integer term)
    {
        System.out.println("courseId: "+courseId+"  term:"+term);
        return studentService.getNotScoreStudentsByCourseIdAndTerm(courseId,term);
    }
}